module mpi_wrap

  use dimensions

  implicit none

!-- MPI parameters

  integer MPI_INTEGER_KIND
  parameter (MPI_INTEGER_KIND = 4)

  integer MPI_REAL8
  parameter (MPI_REAL8 = 15)

  integer MPI_SUM, MPI_MAX, MPI_COMM_WORLD
    
  parameter (MPI_SUM = 1)
  parameter (MPI_MAX = 2)
  parameter (MPI_COMM_WORLD = 0)


  integer  :: myid, mpi_err, mpi_info

  parameter (mpi_err = 0)
  parameter (myid = 0)



  character*120 :: msg



!================================================================================
contains
!================================================================================

  subroutine mpi_wrap_init

    implicit none

    INTEGER    REQUIRED, PROVIDED, IERROR

    if (npDir .ne. 1) then
       write(msg,*) "Serial code. Compiled npDir = 1"
       call mpi_wrap_err
    end if

    write(msg,*) char(10), ' mpi_wrap module initialized'
    call mpi_wrap_msg

    return
  end subroutine mpi_wrap_init

!================================================================================

  subroutine mpi_wrap_exit

    stop

  end subroutine mpi_wrap_exit

!================================================================================

  subroutine mpi_wrap_msg

    use parameters, only : runname
    implicit none

    integer :: fout

    fout = 33
    
    open(fout, file=trim(runname)//'.out', position='append')
    write(fout,*) trim(msg)
    close(fout)

    return
  end subroutine mpi_wrap_msg

!================================================================================

  subroutine mpi_wrap_err

    use parameters, only : runname
    implicit none

    integer :: fout

    fout = 33

    write(*,*) "ERROR: ",  trim(msg)

    open(fout, file=trim(runname)//'.out', position='append')
    write(fout,*) "ERROR: ",  trim(msg)
    close(fout)

    stop

  end subroutine mpi_wrap_err

!================================================================================

  subroutine mpi_abort(comm, flag, err)

    integer, intent(in) ::   comm, flag, err

    stop

  end subroutine mpi_abort

!================================================================================

  subroutine mpi_reduce(datain, dataout, size, type, oper, flag, comm, err)

    integer, intent(in) ::   size, type, oper, comm, flag, err
 
    real, intent(in)  :: datain(size)
    real, intent(out) :: dataout(size)


    dataout = datain


  end subroutine mpi_reduce

!================================================================================

  subroutine mpi_allreduce(datain, dataout, size, type, oper, comm, err)

    integer, intent(in) ::   size, type, oper, comm, err
 
    complex, intent(in)  :: datain(size)
    complex, intent(out) :: dataout(size)

  
    dataout = datain


  end subroutine mpi_allreduce

!================================================================================


end module mpi_wrap
